package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.ApplicationRecord;
import com.woniuxy.factory.entity.OtherIn;
import com.woniuxy.factory.entity.Purchasegoods;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.InventoryMapper;
import com.woniuxy.factory.mapper.OtherInMapper;
import com.woniuxy.factory.mapper.PurchasegoodsMapper;
import com.woniuxy.factory.service.OtherInService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-23 0:02
 */
@Service
public class OtherInServiceImp implements OtherInService {

    @Autowired
    OtherInMapper otherInMapper;

    @Autowired
    ApplicationRecordMapper applicationRecordMapper;

    @Autowired
    InventoryMapper inventoryMapper;

    @Autowired
    PurchasegoodsMapper purchasegoodsMapper;

    //注入库存的service

    @Override
    public List<OtherIn> selectAllOtherIn(Integer page, Integer limit, OtherIn otherIn
            ,String pupId,String searchDate) {
        //调用插入入库表数据的方法
        insertOtherIn();
        //获取起始时间和结束时间
        String startTime =null ;
        String endTime  = null;
        //非空验证
        if(searchDate!=null&&searchDate!=""){
            startTime = searchDate.substring(0,10);
            endTime = searchDate.substring(13,23);
            System.out.println("startTime:"+startTime);
            System.out.println("endTime:"+endTime);
        }
        int home = (page-1)*limit;
        List<OtherIn> otherIns = otherInMapper.selectAllOtherIn(home, limit, otherIn,pupId,startTime,endTime);
        return otherIns;
    }
    //开启事务
    @Transactional
    @Override
    public ResponseResult updateOtherIn(OtherIn otherIn,String pupId) {
        ResponseResult responseResult = new ResponseResult<>();

        //更改状态
        otherIn.setOtherinstatus(AuditType.ALREADY_PASSED);
        //插入日期
        otherIn.setOtherintime(new Date());
        try {
            otherInMapper.updateOtherIn(otherIn,pupId);
            //调用方法更新库存
            addInventory(pupId);
        }catch (RuntimeException e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseResult.setMsg("入库异常，请检查输入是否有错误");
        }
        responseResult.setCode(AuditType.No_Approval);
        responseResult.setMsg("入库成功");

        return responseResult;
    }

    @Override
    public int selectAllCount() {
        int count = otherInMapper.selectAllCount();
        return count;
    }


    //插入入库记录表
    public void insertOtherIn() {
        //创建申请类型记录表对象
        ApplicationRecord applicationRecord = new ApplicationRecord();
        //为申请表类型对象赋上用料申请的值
        applicationRecord.setApplicationtype(ApplicationType.PURCHASEPLAN);
        applicationRecord.setApplystatus(AuditType.ALREADY_PASSED);

        List<ApplicationRecord> applicationRecords = applicationRecordMapper.selectApplicationRecordByCondition(applicationRecord);
        //非空验证
        if(applicationRecords.size()>0&&!applicationRecords.isEmpty()) {
            for (ApplicationRecord record : applicationRecords) {
                int count = otherInMapper.selectCountByPupId(record.getApplicationnum());
                if(count==0){
                    otherInMapper.insertOtherIn(record.getApplicationnum(),1);
                }
            }
        }
    }

    //添加物料的方法
    public  void addInventory(String pupId){

        //根据传过来的采购编号查询所有的采购项
        List<Purchasegoods> purchasegoods = purchasegoodsMapper.selectPurchasegoodsByPupId(pupId);
        for (Purchasegoods purchasegood : purchasegoods) {
            //获得当前物料编号
            String maeId = purchasegood.getMaterial().getMaeId();
            //获得当前物料的数量
            Integer quantity = purchasegood.getQuantity();
            //调用仓库mapper方法更新数量
            inventoryMapper.updateInventoryAmount(quantity,maeId);
        }
    }
}
